package comment

import (
	"community-service/cmd/api/internal/models/model"
	e "community-service/pkg/errorx"
	"context"
	"google.golang.org/grpc/codes"

	"community-service/cmd/api/internal/svc"
	"community-service/cmd/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type TopPostCommentLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTopPostCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TopPostCommentLogic {
	return &TopPostCommentLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TopPostCommentLogic) TopPostComment(req *types.PostCommentTopReq) (resp *types.Empty, err error) {

	var existComment model.CsCommunityPostComment
	if err = l.svcCtx.DB.WithContext(l.ctx).Model(&model.CsCommunityPostComment{}).
		Where("id = ?", req.Id).Find(&existComment).Error; err != nil {
		return nil, e.NewGrpcErr(codes.Internal, e.DBError)
	}

	if existComment.ID == 0 {
		return nil, e.NewGrpcErr(codes.NotFound, e.CommentNotFound)
	}

	if err = l.svcCtx.DB.WithContext(l.ctx).Model(&model.CsCommunityPostComment{}).
		Where("id = ?", req.Id).Update("is_topped", req.Action).Error; err != nil {
		return nil, e.NewGrpcErr(codes.Internal, e.DBError)
	}

	return
}
